package de.hhbk.feedbackmodul.daobeans;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import de.hhbk.feedbackmodul.daointerfaces.QuestionSheetDaoIF;
import de.hhbk.feedbackmodul.entities.QuestionSheet;

@Stateless
public class QuestionSheetDaoBean implements QuestionSheetDaoIF {

	@PersistenceContext(unitName = "FeedbackModulEJB")
	private EntityManager manager;
	
	private Logger logger = Logger.getLogger(this.getClass());
	

	@SuppressWarnings("unchecked")
	@Override
	public List<QuestionSheet> getAllQuestionSheets() throws Exception  {
		try
		{
			Query query = manager.createQuery("select qs from QuestionSheet qs", QuestionSheet.class);
			return (List<QuestionSheet>) query.getResultList();
		}
		catch(Exception e)
		{
			logger.error("Error while reading QuestionSheets: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void createQuestionSheet(QuestionSheet questionSheet) throws Exception {
		try
		{
			manager.persist(questionSheet);
		}
		catch(Exception e)
		{
			logger.error("Error while persisting QuestionSheet: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void updateQuestionSheet(QuestionSheet questionSheet) throws Exception {
		try
		{
			manager.merge(questionSheet);
		}
		catch(Exception e)
		{
			logger.error("Error while updating QuestionSheet: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void deleteQuestionSheet(QuestionSheet questionSheet)
			throws Exception {
		try
		{
			QuestionSheet questionSheetToDelete = manager.find(QuestionSheet.class, questionSheet.getId());
			manager.remove(questionSheetToDelete);
		}
		catch(Exception e)
		{
			logger.error("Error while deleting QuestionSheet: "+e.getMessage(), e);
			throw e;
		}
		
	}

}
